package com.mfangsoft.zhuangjialong.article.service.impl;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mfangsoft.zhuangjialong.app.applogin.mapper.CustomerEntityMapper;
import com.mfangsoft.zhuangjialong.article.mapper.ArticleCollectionEntityMapper;
import com.mfangsoft.zhuangjialong.article.mapper.ArticleEntityMapper;
import com.mfangsoft.zhuangjialong.article.mapper.ArticleEvaluationEntityMapper;
import com.mfangsoft.zhuangjialong.article.mapper.ArticleLikesEntityMapper;
import com.mfangsoft.zhuangjialong.article.model.ArticleDetailModel;
import com.mfangsoft.zhuangjialong.article.model.ArticleEntity;
import com.mfangsoft.zhuangjialong.article.model.ArticleEvaluationEntity;
import com.mfangsoft.zhuangjialong.article.model.ArticleEvaluationModel;
import com.mfangsoft.zhuangjialong.article.service.ServerArticleService;
import com.mfangsoft.zhuangjialong.common.context.UserContext;
import com.mfangsoft.zhuangjialong.common.model.Page;
import com.mfangsoft.zhuangjialong.common.utils.EmojiUtil;
import com.mfangsoft.zhuangjialong.common.utils.JGuangPushUtil;
import com.mfangsoft.zhuangjialong.common.utils.RegexUtils;
import com.mfangsoft.zhuangjialong.core.quartz.Quest;
import com.mfangsoft.zhuangjialong.core.quartz.QuestsManagerBean;
import com.mfangsoft.zhuangjialong.integration.user.mapper.UserEntityMapper;
import com.mfangsoft.zhuangjialong.integration.user.mapper.UserInfoEntityMapper;
import com.mfangsoft.zhuangjialong.integration.user.mapper.UserRelationEntityMapper;
import com.mfangsoft.zhuangjialong.integration.user.model.UserEntity;
import com.mfangsoft.zhuangjialong.integration.user.model.UserRelationEntity;

/**
 * 
 * @author top
 *
 */
@Service
public class ServerArticleServiceImpl implements ServerArticleService{

	@Autowired
	private ArticleEntityMapper articleEntityMapper;
	@Autowired
	private ArticleLikesEntityMapper articleLikesEntityMapper;
	@Autowired
	private ArticleCollectionEntityMapper articleCollectionEntityMapper;
	@Autowired
	private ArticleEvaluationEntityMapper articleEvaluationEntityMapper;
	@Autowired
	private CustomerEntityMapper customerEntityMapper;
	@Autowired
	private UserEntityMapper userEntityMapper;
	@Autowired
	private UserRelationEntityMapper userRelationEntityMapper;
	@Override
	public Page<ArticleDetailModel> queryNews(Page<ArticleDetailModel> page) {
		//判断用户的等级来显示不同的文章
		UserEntity user = UserContext.getCurrentUser();
		Integer user_level = user.getUser_level();
		if(user_level == 0){
			return null;
		}
		//找到创建他的商户
//		Long creater_id = (userEntityMapper.selectByPrimaryKey(user.getId())).getParent_id();
		
		Map<String,Object> m = new HashMap<String,Object>();
		String node = user_level ==3?"node3":"node"+(user_level+1);
		m.put("noden", node);
		m.put("nodet", "node"+user_level);
		m.put("level", user_level);
		m.put("user_id", user.getId());
		Long creater_id = userRelationEntityMapper.selectParentUser(m);
		
		if(user_level == 3 || creater_id  == 153){
			Map<String,Long> map  = new HashMap<String,Long>();
			map.put("sys_user_id", user.getId());
			page.setParam(map);
			return quaryNews(page,true);
		}
		
		if(user_level == 2){
			Map<String,Long> map  = new HashMap<String,Long>();
			map.put("sys_user_id", creater_id);
			page.setParam(map);
			return quaryNews(page,false);
		}
		
		if(user_level == 1){
			Integer creater_level = userEntityMapper.selectByPrimaryKey(creater_id).getUser_level();
			if(creater_level == 3){
				Map<String,Long> map  = new HashMap<String,Long>();
				map.put("sys_user_id", creater_id);
				page.setParam(map);
				return quaryNews(page,false);
			}
			//TODO
			//判断是否是顶级直接创建的
			Map<String,Object> map1 = new HashMap<String,Object>();
			String noden = "node"+user_level;
			map1.put("noden", noden);
			map1.put("user_id", user.getId());
			map1.put("level", user_level);
			UserRelationEntity userRelation = userRelationEntityMapper.selectTopUser(map1);
			if(creater_level == 2 && userRelation.getNode3() ==  userRelation.getNode2()){
				Map<String,Long> map  = new HashMap<String,Long>();
				map.put("sys_user_id", creater_id);
				page.setParam(map);
				return quaryNews(page,false);
			}
			if(creater_level == 2){
				Map<String,Long> map  = new HashMap<String,Long>();
				map.put("sys_user_id", creater_id);
				page.setParam(map);
				return quaryNews(page,false);
			}
		}
		
		return null;
	}
	
	/**
	 * 根据page分页对象查看文章列表
	 * @param page     isTop_level:是否是顶级，true表示是顶级，false表示不是顶级，顶级可以编辑删除和查看，不是顶级只能查看
	 * @return
	 */
	private Page<ArticleDetailModel> quaryNews(Page<ArticleDetailModel> page,Boolean isTop_level) {
		List<ArticleDetailModel> queryNewsList = new ArrayList<ArticleDetailModel>();
		List<ArticleEntity> articles = articleEntityMapper.selectArticlesForPage(page);
		for (ArticleEntity articleEntity : articles) {
			ArticleDetailModel articleDetailModel = new ArticleDetailModel();
			articleDetailModel.setArticle_id(articleEntity.getId());
			articleDetailModel.setArticle_title(articleEntity.getTitle());
			articleDetailModel.setArticle_createTime(articleEntity.getCreate_time());
			articleDetailModel.setArticle_createTime1(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(articleEntity.getCreate_time()));
			articleDetailModel.setLikes_num(articleLikesEntityMapper.getArticleLikesNumByArticleId(articleEntity.getId()));
			articleDetailModel.setCollect_num(articleCollectionEntityMapper.getCollectNumByArticleId(articleEntity.getId()));
			articleDetailModel.setEvaluation_num(articleEvaluationEntityMapper.getEvaluationNumByArticleId(articleEntity.getId()));
			articleDetailModel.setShare_num(articleEntity.getShare_num());
			queryNewsList.add(articleDetailModel);
		}
		page.setData(queryNewsList);
		page.setIsTop_level(isTop_level);
		return page;
	}
	
	
	@Override
	public boolean addArticle(ArticleEntity articleEntity) {
		articleEntity.setCreate_time(new Date());
		articleEntity.setState(1);
		articleEntity.setUpdate_time(articleEntity.getCreate_time());
		articleEntity.setUser_id(UserContext.getCurrentUserId());
		int num = articleEntityMapper.insertSelective(articleEntity);
		JGuangPushUtil.sendPush(articleEntity.getTitle(), articleEntity.getId());
		return num==0?false:true;
	}
	@Override
	public boolean updateArticle(ArticleEntity articleEntity) {
		articleEntity.setUpdate_time(new Date());
		return articleEntityMapper.updateByPrimaryKeySelective(articleEntity)==0?false:true;
	}
	@Override
	public boolean deleteArticleByArticleId(Long articleId) {
		return articleEntityMapper.deleteByPrimaryKey(articleId)==0?false:true;
	}
	@Override
	public ArticleEntity queryarticledetails(Long articleId) {
		return articleEntityMapper.selectByPrimaryKey(articleId);
	}
	
	@Override
	public ArticleDetailModel queryArticleDetails(ArticleDetailModel param) {
      QuestsManagerBean.addQuest(new Quest() {
			
			@Override
			public boolean execute() {
				int i = articleEntityMapper.updateSeenTime(param.getId());
				return i>0;
			}
			
			@Override
			public boolean delete() {
				return true;
			}
			
			@Override
			public boolean condition() {
				return true;
			}
		});
		return articleEntityMapper.selectDetailsByPrimaryKey(param.getId());
	}
	
	@Override    
	public Page<ArticleEvaluationModel> queryAppArticleEvaluationForPage(Page<ArticleEvaluationModel> page) {
		//找到他的顶级，在根据顶级找到顶级发的文章id
		UserEntity user = UserContext.getCurrentUser();
		Long id = user.getId();
		Integer user_level = user.getUser_level();
		Map<String,Object> map = new HashMap<String,Object>();
		String noden = "node"+user_level;
		map.put("noden", noden);
		map.put("user_id", id);
		map.put("level", user_level);
		UserRelationEntity userRelationEntity = userRelationEntityMapper.selectTopUser(map);
		Long node3 = null;
		if(userRelationEntity != null){
			node3 = userRelationEntity.getNode3();
		}
		Map<String,Object> map1 = new HashMap<String,Object>();
		map1.put("top_id", node3);
		page.setParam(map1);
		List<ArticleEvaluationModel> articleEvaluationModels = new ArrayList<ArticleEvaluationModel>();
		List<ArticleEvaluationEntity> articleEvaluationLists = articleEvaluationEntityMapper.selectAppArticleEvaluationsForPage(page);
		for (ArticleEvaluationEntity articleEvaluationEntity : articleEvaluationLists) {
			ArticleEvaluationModel articleEvaluationModel = new ArticleEvaluationModel();
			articleEvaluationModel.setId(articleEvaluationEntity.getId());
			articleEvaluationModel.setTitle(articleEntityMapper.selectByPrimaryKey((long)articleEvaluationEntity.getArticle_id()).getTitle());
			String customer_name =  customerEntityMapper.selectCustomerByCustomer_id(articleEvaluationEntity.getCustomer_id()).getName();
			if(RegexUtils.matcher(RegexUtils.PHONE_REGEX, customer_name)){
				customer_name = customer_name.substring(0,3)+"****"+customer_name.substring(7, 11);
				articleEvaluationModel.setCustomer_name(customer_name);
			}
			articleEvaluationModel.setCustomer_name(customer_name);
			articleEvaluationModel.setCreate_time(articleEvaluationEntity.getCreate_time());
			try {
				articleEvaluationModel.setContent(EmojiUtil.resolveToEmoji(articleEvaluationEntity.getContent()));
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			//设置评论的回复量
			Integer position_num = articleEvaluationEntityMapper.selectPosition_numByEvaluationId(articleEvaluationEntity.getId());
			articleEvaluationModel.setPosition_num(position_num);
			articleEvaluationModel.setState(articleEvaluationEntity.getState());
			articleEvaluationModels.add(articleEvaluationModel);
			if(id == node3)
				articleEvaluationModel.setIsEditer(true);
			else
			    articleEvaluationModel.setIsEditer(false);
		}
		page.setData(articleEvaluationModels);
		return page;
	}
	
	@Override
	public Page<ArticleEvaluationModel> queryevaluationManagement(Page<ArticleEvaluationModel> page) {
		//找到他的顶级，在根据顶级找到顶级发的文章id
		UserEntity user = UserContext.getCurrentUser();
		Long id = user.getId();
		Integer user_level = user.getUser_level();
		Map<String,Object> map = new HashMap<String,Object>();
		String noden = "node"+user_level;
		map.put("noden", noden);
		map.put("user_id", id);
		map.put("level", user_level);
		UserRelationEntity userRelationEntity = userRelationEntityMapper.selectTopUser(map);
		Long node3 = null;
		if(userRelationEntity != null){
			node3 = userRelationEntity.getNode3();
		}
		Map<String, Object> map1=(Map<String, Object>)page.getParam();
		map1.put("top_id", node3);
		page.setParam(map1);
		List<ArticleEvaluationModel> queryevaluationManagementList = articleEvaluationEntityMapper.queryevaluationManagementForPage(page);
		List<ArticleEvaluationModel> queryevaluationManagementList1 = new ArrayList<ArticleEvaluationModel>();
		for (ArticleEvaluationModel articleEvaluationModel : queryevaluationManagementList) {
			ArticleEvaluationModel articleEvaluationModel1 = new ArticleEvaluationModel();
			articleEvaluationModel1.setEvaluation_id(articleEvaluationModel.getEvaluation_id());
			articleEvaluationModel1.setTitle(articleEvaluationModel.getTitle());
			articleEvaluationModel1.setCustomer_name(articleEvaluationModel.getCustomer_name());
			articleEvaluationModel1.setCreate_time(articleEvaluationModel.getCreate_time());
			articleEvaluationModel1.setContent(articleEvaluationModel.getContent());
			//设置评论的回复量
			Integer position_num = articleEvaluationEntityMapper.selectPosition_numByEvaluationId(articleEvaluationModel.getEvaluation_id());
			articleEvaluationModel1.setPosition_num(position_num);
			if(id == node3){
				articleEvaluationModel1.setIsEditer(true);
			}else{
				articleEvaluationModel1.setIsEditer(false);
			}
			queryevaluationManagementList1.add(articleEvaluationModel1);
		}
		page.setData(queryevaluationManagementList1);
		return page;
	}
	@Override
	public boolean updateEvaluationStateByEvaluationId(Long evaluationId,Long state) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("evaluationId", evaluationId);
		map.put("state", state);
		return articleEvaluationEntityMapper.updateEvaluationStateByEvaluationId(map)==0?false:true;
	}
	
	public static void main(String[] args) {
		JGuangPushUtil.sendPush("测试推送", 23l);
	}
	
	
}
